package com.example.app4;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.example.app4.adapter.MyAdapter;
import com.example.app4.service.TestService3;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private MyAdapter myAdapter;

    TestService3.MyBinder binder;

    //集合
    public static ListView lv_show;
    private Button xiaog,xiaomao,xiangjia;
    private Intent it1;


    /**
     * 接收service传过来的数值
     */
    private ServiceConnection conn = new ServiceConnection() {


        //Activity与Service连接成功时回调该方法
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder = (TestService3.MyBinder) service;
            System.out.println("------Service Connected-------");

        }

        //Activity与Service断开连接时回调该方法
        @Override
        public void onServiceDisconnected(ComponentName name) {

            System.out.println("------Service DisConnected-------");
        }

    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //数据集合
        lv_show = findViewById(R.id.lv_show);

        //小狗按钮
        xiaog = findViewById(R.id.tv_title);
        xiaomao = findViewById(R.id.tv_xiaomao);
        xiangjia = findViewById(R.id.tv_xiangjiao);


        xiaog.setOnClickListener(this);
        xiaomao.setOnClickListener(this);
        xiangjia.setOnClickListener(this);

        it1 = new Intent();
        it1.setAction("com.test.intentservice");
        it1.setPackage(getPackageName());
        Bundle b1 = new Bundle();
        b1.putString("param", "s1");
        b1.putInt("shuju",10000);
        it1.putExtras(b1);
        //绑定service
        bindService(it1, conn, Service.BIND_AUTO_CREATE);

        Intent it2 = new Intent();
        it2.setAction("com.test.intentservice");
        it2.setPackage(getPackageName());
        Bundle b2 = new Bundle();
        b2.putString("param", "s2");
        it2.putExtras(b2);

        Intent it3 = new Intent();
        it3.setAction("com.test.intentservice");
        it3.setPackage(getPackageName());
        Bundle b3 = new Bundle();
        b3.putString("param", "s3");
        it3.putExtras(b3);

        //接着启动多次IntentService,每次启动,都会新建一个工作线程
        //但始终只有一个IntentService实例
      /*  startService(it1);
        startService(it2);
        startService(it3);*/

      //  likeListView();
    }




    //狗
    public void likeListView() {

        myAdapter = new MyAdapter(binder.getCountxiaog(), MainActivity.this);
        //把封装好的数据展示到标签listVine中

    }

    //香蕉
    public void likeListViewxiangjiao() {

        myAdapter = new MyAdapter(binder.getCountxiangjiao(), MainActivity.this);
        //把封装好的数据展示到标签listVine中

    }


    public void likeListViewxiaomao() {

        myAdapter = new MyAdapter(binder.getCountxiaomao(), MainActivity.this);
        //把封装好的数据展示到标签listVine中


    }


    public void xx() {
        lv_show.setAdapter(myAdapter);
    }

    @Override
    public void onClick(View v) {

        //狗
        if (v.getId() == R.id.tv_title) {
            startService(it1);
           likeListView();

          //猫
        } /*else if (v.getId() == R.id.tv_xiaomao) {

         //   likeListViewxiaomao();

            //香蕉
        } else if (v.getId()==R.id.tv_xiangjiao) {

          //  likeListViewxiangjiao();

        }*/

        xx();
    }
}